package com.apexedu.portal.controller;

import com.alibaba.fastjson.JSON;
import com.apexedu.biz.service.impl.BizTreeSupport;
import com.apexedu.framework.util.DBUtil;
import com.apexedu.identity.IdConstants;
import com.apexedu.identity.entity.TSysUser;
import com.apexedu.portal.constant.ActionConstant;
import com.apexedu.portal.entity.TNavLink;
import com.apexedu.portal.service.impl.PortalNavLinkServiceImpl;
import com.apexedu.portal.util.DBMUtil;
import com.apexedu.util.DateUtil;
import com.apexedu.util.StringUtil;
import com.apexedu.vo.CommonPojo;
import com.apexedu.vo.TjxJskbxx;
import com.apexedu.vo.TjxPksjmx;
import net.ysen.tree.entitybean.TreeNode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by liwang on 2017/7/3.
 */
@Controller
public class MhSzgdzyxxController extends BaseController {
    Logger logger = LoggerFactory.getLogger(MhSzgdzyxxController.class);
    /**
     * 获取资源菜单数据
     *
     * @param appId
     * @param upMenuId
     * @return
     */
    @Autowired
    private PortalNavLinkServiceImpl portalNavLinkService;

    @ResponseBody
    @RequestMapping(value = ActionConstant.SzGz.Menu_tree)
    public String getOneCardPassHistoricalLists(String appId, String upMenuId) {
        List<TreeNode> result = null;
        try {
            result = BizTreeSupport.queryTreeData(appId, upMenuId);
        } catch (SQLException e) {
            logger.error("生成菜单树失败!", e);
        }
        return JSON.toJSONString(result);
    }

    /**
     * 获取导航链接菜单数据
     */
    @ResponseBody
    @RequestMapping(value = ActionConstant.SzGz.NavLink_list)
    public String getNavLink_list() {
        List<TNavLink> result = portalNavLinkService.list();
        return JSON.toJSONString(result);
    }

    /**
     * 获取排课信息
     * 如果是老师根据老师id查
     * 如果只是学生，查询班级课程
     */
    @RequestMapping(value = ActionConstant.SzGz.RequestMapping_pk)
    public String getPK(ModelMap map, HttpServletRequest request) {
        try {
            DBMUtil dbmUtil = new DBMUtil(ActionConstant.SzGz.DATA_SOURCE_JX);
            TSysUser user = (TSysUser) request.getSession().getAttribute(IdConstants.SESSION_USER);
            String jgid = getXgId(dbmUtil, user.getUserloginid());
            String xqid = getXqId(dbmUtil);

            List<CommonPojo> returnlist = new ArrayList<CommonPojo>();
            List<TjxPksjmx> pkmxList = new ArrayList<TjxPksjmx>();

            if(StringUtil.isNotEmpty(jgid)&&StringUtil.isNotEmpty(xqid)){
                //1.教学任务 非合并教学
                String list1 = "select kc.kcmc," +
                        "       (select LISTAGG(qzz.mc, ',') within group (order by id)" +
                        "          from tjx_qzzb qzz" +
                        "         where ';' || jxrw.qzz || ';' like '%;' || qzz.id || ';%') as qzzmc," +
                        "       bj.xzbmc," +
                        "       mx.xqj," +
                        "       mx.djdj," +
                        "       mx.djj," +
                        "       case mx.jtjc" +
                        "         when 1 then" +
                        "          '2节'" +
                        "         else" +
                        "          '1节'" +
                        "       end as jj," +
                        "       jxcd.jsmc" +
                        "  from tjx_jhjxb jxrw, tjx_kc_gxjx02 kc, t_bj_gxxx04 bj, TJX_PKSJMX mx" +
                        "  left join t_js_gxjx0601 jxcd" +
                        "    on mx.jxcd = jxcd.id" +
                        " where jxrw.kc = kc.id" +
                        "   and jxrw.bj = bj.id" +
                        "   and mx.jxrw = jxrw.id" +
                        "   and jxrw.zjjs = ?" +
                        "   and jxrw.xq = ?" +
                        "   and jxrw.shzt = 99" +
                        "   and jxrw.rwhbjx is null";

                //2.教学任务 合并教学的
                String list2 = "select kc.kcmc," +
                        "       (select LISTAGG(qzz.mc, ',') within group (order by id)" +
                        "          from tjx_qzzb qzz" +
                        "         where ';' || jxrw.qzz || ';' like '%;' || qzz.id || ';%') as qzzmc," +
                        "       jxrw.rwhbqk as xzbmc," +
                        "       jxrw.xqj," +
                        "       jxrw.djdj," +
                        "       jxrw.djj," +
                        "       (case jxrw.jtjc" +
                        "         when 1 then" +
                        "          '2节'" +
                        "         else" +
                        "          '1节'" +
                        "       end) as jj," +
                        "       jxcd.jsmc" +
                        "  from (select rw.kc," +
                        "               rw.qzz," +
                        "               rw.rwhbjx," +
                        "               rw.rwhbqk," +
                        "               mx.xqj," +
                        "               mx.djdj," +
                        "               mx.djj," +
                        "               mx.jtjc," +
                        "               nvl(mx.jxcd,0) jxcd" +
                        "          from tjx_jhjxb rw, TJX_PKSJMX mx" +
                        "         where mx.jxrw = rw.id" +
                        "           and rw.zjjs = ?" +
                        "           and rw.xq = ?" +
                        "           and rw.shzt = 99" +
                        "           and rw.rwhbjx is not null" +
                        "         group by rw.kc," +
                        "                  rw.qzz," +
                        "                  rw.rwhbjx," +
                        "                  rw.rwhbqk," +
                        "                  mx.xqj," +
                        "                  mx.djdj," +
                        "                  mx.djj," +
                        "                  mx.jtjc," +
                        "                  mx.jxcd) jxrw" +
                        "       left join tjx_kc_gxjx02 kc on jxrw.kc = kc.id" +
                        "       left join t_js_gxjx0601 jxcd on jxrw.jxcd = jxcd.id" +
                        " where 1=1" ;


                //3.公共选修课 任务
                String list3 = "select rw.jxbmc as kcmc," +
                        "       (select LISTAGG(qzz.mc, ',') within group (order by id)" +
                        "          from tjx_qzzb qzz" +
                        "         where ';' || rw.qzz || ';' like '%;' || qzz.id || ';%') as qzzmc," +
                        "       '公选' as xzbmc," +
                        "       sj.xqj," +
                        "       sj.djdj," +
                        "       sj.djj," +
                        "       case sj.jtjc" +
                        "         when 1 then" +
                        "          '2节'" +
                        "         else" +
                        "          '1节'" +
                        "       end as jj," +
                        "       jxcd.jsmc" +
                        "  from TJX_XXKSKSJ sj, TJX_XXKJXRW rw" +
                        "  left join t_js_gxjx0601 jxcd" +
                        "    on rw.skdd = jxcd.id" +
                        " where ';' || rw.sksj || ';' like '%;' || sj.id || ';%'" +
                        "   and rw.zjjs = ?" +
                        "   and rw.xq = ?";

                //4.体育课任务
                String list4 = "select kc.kcmc," +
                        "       null as qzzmc," +
                        "       '板块' as xzbmc," +
                        "       sj.xqj," +
                        "       sj.djdj," +
                        "       sj.djj," +
                        "       case sj.jtjc" +
                        "         when 1 then" +
                        "          '2节'" +
                        "         else" +
                        "          '1节'" +
                        "       end as jj," +
                        "       jxcd.jsmc" +
                        "  from TJX_TYXXAP    ap," +
                        "       TJX_TYXXKC    kc," +
                        "       t_js_gxjx0601 jxcd," +
                        "       TJX_BKXXSZ    sz," +
                        "       TJX_BKKSKSJ   sj" +
                        " where ap.tyxxkc = kc.id" +
                        "   and ap.jxcd = jxcd.id" +
                        "   and ap.bkxxsz = sz.id" +
                        "   and sz.sksj = sj.id" +
                        "   and ap.rkjs = ?" +
                        "   and sz.xq = ?";

                pkmxList = dbmUtil.queryAllBeanList("("+list1+")union all("+list2+")union all("+list3+")union all("+list4+")"
                        ,TjxPksjmx.class,jgid,xqid,jgid,xqid,jgid,xqid,jgid,xqid);
            }

            for(int i = 1;i < 5; i++){//节次
                CommonPojo cp_i = new CommonPojo();
                cp_i.setFlag(i);
                cp_i.setList1(new ArrayList<CommonPojo>());
                for(int j = 1;j < 6; j++){//周几
                    CommonPojo cp_j = new CommonPojo();
                    cp_j.setFlag(j);
                    cp_j.setList1(new ArrayList<TjxJskbxx>());
                    for(TjxPksjmx mx :pkmxList){
                        //节次和周几同时满足
                        if(mx.getDjdj()==i&&mx.getXqj()==j){
                            cp_j.getList1().add(mx);
                        }
                    }
                    cp_i.getList1().add(cp_j);
                }
                returnlist.add(cp_i);
            }
            map.put("returnlist",returnlist);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ActionConstant.SzGz.Forward_pk;
    }

    /**
     * 根据登录名获取教工ID
     *
     * @param dbmUtil
     * @return
     */
    public String getXgId(DBMUtil dbmUtil, String loginId) throws Exception{
        String sql = " select id from GXJG0101 jg  where jg.gh = ?";
        Object field;
        try{
            field = dbmUtil.queryFieldValue(sql, loginId);
        }catch (Exception e){
            log.error(e);
            return null;
        }
        if(field != null){
            String jgId = field.toString();
            return jgId;
        }
        return null;
    }

    /**
     * 查询当前日期所属学期id
     *
     * @return
     */
    public String getXqId(DBMUtil dbmUtil) throws Exception {
        String sql = "select ID from t_xq where zt=2";//查询当前日期所属学期
//        String sql = "select id from t_xq xq where xq.kxrq < ? and xq.bxrq > ?";//查询当前日期所属学期
//        String dateStr = DateUtil.formatDate(new Date(), "yyyyMMdd");
//        String dateStr = "20171022";
        Object fid;
        try{
            fid = dbmUtil.queryFieldValue(sql);
        }catch (Exception e){
            log.error(e);
            return null;
        }
        if(fid != null){
            String xqId = fid.toString();
            return xqId;
        }
        return  null;
    }

}
